package pod

import (
	"context"
	"fmt"

	"go-pod/api/internal/svc"
	"go-pod/api/internal/types"
	"go-pod/pod-rpc/pod"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreatePodcastLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreatePodcastLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePodcastLogic {
	return &CreatePodcastLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreatePodcastLogic) CreatePodcast(req *types.PodCreatReq) (resp *types.PodCreatResp, err error) {
	// 参数校验
	if req.Title == "" {
		return nil, fmt.Errorf("标题不能为空")
	}
	if req.Content == "" {
		return nil, fmt.Errorf("内容不能为空")
	}
	if req.Author == "" {
		return nil, fmt.Errorf("作者不能为空")
	}

	// 调用RPC服务创建播客
	_, err = l.svcCtx.PodRpcClient.CreatePodcast(l.ctx, &pod.CreatePodcastReq{
		Title:   req.Title,
		Content: req.Content,
		Author:  req.Author,
		Tags:    "",
	})
	if err != nil {
		l.Logger.Errorf("创建播客失败: %v", err)
		return nil, fmt.Errorf("创建播客失败，请稍后重试")
	}

	// 记录成功日志
	l.Logger.Infof("播客创建成功，标题: %s, 作者ID: %d", req.Title, req.Author)

	return &types.PodCreatResp{}, nil
}
